<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
  <style>
    .badge {
      float: right;
      margin-right: 5px;
    }
    .my-page{
      margin: 0 5px;
    }
    .all-page-content{
      margin-left: 5px;
    }
  </style>
</head>

<body style="padding: 15px;">

  <!-- 评论面板 -->
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h3 class="panel-title">发表评论</h3>
    </div>
    <form class="panel-body cmt-form">
      <div>评论人：</div>
      <input type="text" class="form-control" name="username" autocomplete="off" />
      <div>评论内容：</div>
      <textarea class="form-control" name="content"></textarea>
      <button type="submit" class="btn btn-primary submit">发表评论</button>
    </form>
  </div>


  <!-- 评论列表 -->
  <ul class="list-group">
    <!-- <li class="list-group-item">
       <span>评论内容</span>
       <span class="badge del" style="cursor:pointer; background-color: lightgray;">删除</span> 
       <span class="badge" style="background-color: #F0AD4E;">评论时间：xxx</span>
       <span class="badge" style="background-color: #5BC0DE;">评论人：xxx</span>
    </li> -->
  </ul>

  <!-- 分页器 -->
  <nav >
    <ul class="pagination">
      <li>
        <button class="last">
          <span>&laquo;</span>
        </button>
      </li>
      <li class="my-page">
        <span class="page-show"></span>
      </li>
      <li>
        <button class="next">
          <span>&raquo;</span>
        </button>
      </li>
      <li class="all-page-content">
        <span>共计:<span class="all-page"></span>页</span>
      </li>
    </ul>
  </nav>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script src="./lib/form-serialize.js"></script>
  <script>
   
  // 1. 默认上来展示所有评论列表数据（注意不区分用户了）感受下大家数据互相影响，也可以看到别人评论
  // 2. 新增评论功能
  // 3. 删除评论功能
  // 4. 分页切换评论列表数据功能
  // 5. 删除最后一条评论，列表要自动回到上一页

  let nowPage = 1
  let allPage
  function getAllComment() {
    nowPage = 1
    axios({
      url: `https://hmajax.itheima.net/api/cmtlist?page=${nowPage}`
    }).then(result => {
      console.log(result)
      // 1.3 保存当前一共有多少页
      allPage = result.data.allPage
      document.querySelector('.all-page').innerHTML = allPage

      // 1.4 先清空当前列表页面 - 重新铺设
      document.querySelector('.list-group').innerHTML = ''
      const commentList = result.data.data.map(item => {
        return `<li class="list-group-item">
          <span>${item.content}</span>
          <span class="badge del" style="cursor:pointer; background-color: lightgray;" data-id='${item.id}'>删除</span> 
          <span class="badge" style="background-color: #F0AD4E;">评论时间：${item.time}</span>
          <span class="badge" style="background-color: #5BC0DE;">评论人：${item.username}</span>
        </li> `
      }).join('')
      document.querySelector('.list-group').innerHTML = commentList
      // 1.6 设置页码
      document.querySelector('.page-show').innerHTML = nowPage
    })
  }
  getAllComment()

  // 4. 分页切换评论列表数据功能
  document.querySelector('.last').addEventListener('click', () => {
    if (nowPage > 1) nowPage--
    getAllComment()
  })

  document.querySelector('.next').addEventListener('click', () => {
    if (nowPage < allPage) nowPage++
    getAllComment()
  })



  //2. 新增评论功能
  document.querySelector('.submit').addEventListener('click', e => {
    //阻止默认行为
    e.preventDefault()
    //2.获取表单数据
    const form = document.querySelector('.cmt-form')
    const data = serialize(form, { hash: true, empty: true})
    //发送给服务器
    axios({
      url: 'https://hmajax.itheima.net/api/addcmt',
      method: 'POST',
      data
    }).then(result => {
      console.log(result)
      nowPage = 1
      getAllComment()
      form.reset()
      
    })
  })

  //3. 删除评论功能
  document.querySelector('.list-group').addEventListener('click', e => {
    // console.log(e.target)
    if (e.target.classList.contains('del')) {
      const theId = e.target.dataset.id
      axios({
        url: `https://hmajax.itheima.net/api/delcmt?id=${theId}`
      }).then(result => {
        console.log(result)
        // 5. 删除最后一条评论，列表要自动回到上一页
        allPage = result.data.allPage
        if (nowPage > allPage) nowPage = allPage
        getAllComment()
      })
    }
    
  })

  
  </script>
</body>

</html>